﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ATCSim.Objects
{
    class Airport
    {
        private Timer _t;
        Random rand = new Random(DateTime.Now.Second);
        private AircraftManager _AirManager;
        private ListBox _ListBox;
        private List<Aircraft> _Que = new List<Aircraft>();
        private List<Aircraft> _FlyingOutOfAirspace = new List<Aircraft>();


        public List<Aircraft> Que
        {
            get { return _Que; }
        }

        public Airport(AircraftManager AircraftManager, ListBox Listbox)
        {
            _AirManager = AircraftManager;
            _ListBox = Listbox;

            _t = new Timer();
            _t.Interval = 3000; //60000 is default for correct ratio of movement
            _t.Tick += new System.EventHandler(TimerOnTick);
            _t.Enabled = true;

            TimerOnTick(null, null);
            TimerOnTick(null, null);
            TimerOnTick(null, null);
        }
        public void TakeOff(String AircraftID)
        {
            _AirManager.AircraftList.Add(FindAircraft(AircraftID));
            _FlyingOutOfAirspace.Add(FindAircraft(AircraftID));
        }
        private void TimerOnTick(object sender, System.EventArgs e)
        {
            if(sender == null && e == null)
                _t.Interval = 3000;
            else
                _t.Interval = rand.Next(60000, 120000);

            String[] idPickList = { "NW", "AA", "DA", "ME", "AW", "UA", "SW", "CA", "PA", "PK", "PD", "RK", "RD" };
            String airlineIDNum = idPickList[rand.Next(0, 12)];
            airlineIDNum += rand.Next(100, 999);
            while (AircraftIDExsists(airlineIDNum))
            {
                airlineIDNum = idPickList[rand.Next(0, 12)];
                airlineIDNum += rand.Next(1, 999);
            }
            _Que.Add(new Aircraft(airlineIDNum, new PointF(602, 365), 360, 1001, "D"));
            FindAircraft(airlineIDNum).DestinationAirport = "MOT";
            _ListBox.Items.Add(airlineIDNum);
        }
        public bool AircraftIDExsists(String ID)
        {
            for (int i = 0; i < _Que.Count; i++)
            {
                if (_Que.ElementAt(i).ID.CompareTo(ID.ToUpper()) == 0)
                    return true;
            }
            return false;
        }
        public Aircraft FindAircraft(String ID)
        {
            for (int i = 0; i < _Que.Count; i++)
            {
                if (_Que.ElementAt(i).ID.CompareTo(ID.ToUpper()) == 0)
                    return _Que.ElementAt(i);
            }
            return null;
        }
        public void FlyOutOfAirspaceUpdate()
        {
            //need to fix bug where planes always take off same direction
            bool tmp = false;
            bool tmp1 = false;
            for (int i = 0; i < _FlyingOutOfAirspace.Count; i++)
            {
                if (_FlyingOutOfAirspace.ElementAt(i).Position.Y <= 275)
                {
                    _FlyingOutOfAirspace.ElementAt(i).ToAltitude = 5000;
                    tmp = true;
                }
                if (_FlyingOutOfAirspace.ElementAt(i).Position.X >= 800)
                    tmp = true;
                if (_FlyingOutOfAirspace.ElementAt(i).Position.X <= 400)
                    tmp = true;
                if (_FlyingOutOfAirspace.ElementAt(i).Position.Y >= 280 && _FlyingOutOfAirspace.ElementAt(i).Position.Y <= 290 &&
                    _FlyingOutOfAirspace.ElementAt(i).Position.X >= 600 && _FlyingOutOfAirspace.ElementAt(i).Position.X <= 620)
                    tmp1 = true;
                else
                    tmp1 = false;
                if (rand.Next(2) == 1 && tmp1 == true)
                {
                    if (_FlyingOutOfAirspace.ElementAt(i).Position.Y <= 285)
                    {
                        _FlyingOutOfAirspace.ElementAt(i).ToHeading = 270;
                        _FlyingOutOfAirspace.ElementAt(i).TurnLeft = true;
                    }
                }
                else if (tmp1 == true)
                {
                    if (_FlyingOutOfAirspace.ElementAt(i).Position.Y <= 285)
                    {
                        _FlyingOutOfAirspace.ElementAt(i).ToHeading = 90;
                        _FlyingOutOfAirspace.ElementAt(i).TurnLeft = false;
                    }
                }
                if (_FlyingOutOfAirspace.ElementAt(i).Position.X >= 800 && tmp == true)
                {
                    if (_FlyingOutOfAirspace.ElementAt(i).Position.Y > 325)
                    {
                        _FlyingOutOfAirspace.ElementAt(i).ToHeading = 90;
                        _FlyingOutOfAirspace.ElementAt(i).TurnLeft = true;
                        _FlyingOutOfAirspace.RemoveAt(i);
                        return;
                    }
                    else
                    {
                        _FlyingOutOfAirspace.ElementAt(i).ToHeading = 180;
                        _FlyingOutOfAirspace.ElementAt(i).TurnLeft = false;
                    }
                }
                if (_FlyingOutOfAirspace.ElementAt(i).Position.X < 400 && tmp == true)
                {
                    if (_FlyingOutOfAirspace.ElementAt(i).Position.Y > 325)
                    {
                        _FlyingOutOfAirspace.ElementAt(i).ToHeading = 270;
                        _FlyingOutOfAirspace.ElementAt(i).TurnLeft = false;
                        _FlyingOutOfAirspace.RemoveAt(i);
                        return;
                    }
                    else
                    {
                        _FlyingOutOfAirspace.ElementAt(i).ToHeading = 180;
                        _FlyingOutOfAirspace.ElementAt(i).TurnLeft = true;
                    }
                }
                tmp = false;
            }
        }
    }
}
